//
//  DryCleaningViewController.m
//  YaDiuDiu
//
//  Created by 陈刚 on 2017/10/10.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "DryCleaningViewController.h"
#import "RentalDetailsTableViewCell.h"
#import "OrderProductTableViewCell.h"

@interface DryCleaningViewController ()<BMKMapViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *bgscroll;

@property (weak, nonatomic) IBOutlet BMKMapView *topMapView;

@property (weak, nonatomic) IBOutlet UILabel *orderStatusLabel;

@property (weak, nonatomic) IBOutlet UIButton *contentButton;

@property (weak, nonatomic) IBOutlet UILabel *counPricLabel;

@property (weak, nonatomic) IBOutlet UILabel *companyName;

@property (weak, nonatomic) IBOutlet UITableView *orderTable;

@property (weak, nonatomic) IBOutlet UITableView *detalsTable;

- (IBAction)contentEvent:(UIButton *)sender;

- (IBAction)aginEvent:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *onetabbottomheight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *twotableHeight;

@property (nonatomic, strong) NSDictionary *orderTypeDic;

@property (nonatomic, strong) NSMutableArray *productArray;

@property (nonatomic, strong) NSMutableArray *tableTitleArray;

@property (nonatomic, strong) NSMutableArray *detalArray;

@property (nonatomic, strong) NSDictionary *backPayTypeDic;

@end

@implementation DryCleaningViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    [self loadData];
    
    
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.topMapView viewWillAppear];
    [self.topMapView setDelegate:self];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.topMapView viewWillDisappear];
    [self.topMapView setDelegate:nil];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.twotableHeight.constant = 264;
    [self.bgscroll setContentSize:CGSizeMake(ScreenWidth, self.detalsTable.frame.origin.y + self.detalsTable.frame.size.height + 44)];
}

- (void)setupView{
    [self showNav:@"订单详情"];
    
    self.onetabbottomheight.constant = 0;
    
    [self.contentButton.layer setMasksToBounds:YES];
    [self.contentButton.layer setCornerRadius:5];
    [self.contentButton.layer setBorderWidth:1];
    [self.contentButton.layer setBorderColor:HexColorInt32_t(FD1142).CGColor];
    
    [self.orderTable registerNib:[UINib nibWithNibName:@"OrderProductTableViewCell" bundle:[NSBundle mainBundle]] forHeaderFooterViewReuseIdentifier:@"OtherCell"];
    [self.detalsTable registerNib:[UINib nibWithNibName:@"RentalDetailsTableViewCell" bundle:[NSBundle mainBundle]] forHeaderFooterViewReuseIdentifier:@"Cell"];
    
    
    NSString *ordertype = [self.detailsDic objectForKey:@"typeid"];
    if ([ordertype integerValue] == 10) {
        self.orderTypeDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"等待接单",@"2",@"接单",@"3",@"等骑手接单",@"4",@"取件中",@"5",@"运输中",@"6",@"完成",@"7",@"评价",@"8",@"配送抢单池",@"9",@"重新呼叫骑手",@"10",@"取消订单中",@"11",@"已取消",@"12", nil];
    }else{
        self.orderTypeDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"等待接单",@"2",@"接单",@"3",@"等骑手接单",@"4",@"取件中",@"5",@"运输中",@"6",@"正在洗衣",@"7",@"洗衣完成",@"8",@"等待骑手接单",@"9",@"取件中",@"10",@"配送中",@"11",@"配送完成",@"12",@"完成",@"13",@"取货抢单池",@"14",@"配送抢单池",@"15",@"用户重新呼叫骑手",@"16",@"商家重新呼叫",@"17",@"取消订单中",@"18",@"已取消",@"19", nil];
    }
    
    
    self.tableTitleArray = [[NSMutableArray alloc]initWithObjects:@"订 单 号",@"下单时间",@"支付方式",@"收货地址", nil];
    
    self.backPayTypeDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"微信",@"2",@"支付宝",@"3",@"银联",@"4",@"余额",@"5", nil];
    
}

- (void)loadData{
    NSString *ordertype = [_detailsDic objectForKey:@"typeid"];
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSString *orderid = [self.detailsDic objectForKey:@"id"];
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:token forKey:@"verifyToken"];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setValue:orderid forKey:@"orderid"];
    [parmas setValue:ordertype forKey:@"orderTypeId"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/UserOrder/userOrderDetails",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"用户查看订单列表res:%@",responseObject);
        NSInteger state = [[responseObject objectForKey:@"status"] integerValue];
        NSString *msg = [responseObject objectForKey:@"msg"];
        NSDictionary *dataDic = [responseObject objectForKey:@"content"];
        if (state  == 200 && [dataDic isKindOfClass:[NSDictionary class]]) {
            [SVProgressHUD dismiss];
            NSString *orderdenumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [self getyYearsTimeFormat:[dataDic objectForKey:@"addtime"]];
            
            
            NSString *ordername = [dataDic objectForKey:@"ordername"];
            NSString *addressString = [NSString stringWithFormat:@"%@",[dataDic objectForKey:@"address"]];
            NSString *payTypeString = [dataDic objectForKey:@"ordertype"];
            NSString *pplayType = [self.backPayTypeDic objectForKey:payTypeString];
            
            NSString *orderstateString = [dataDic objectForKey:@"orderstate"];
            [self.orderStatusLabel setText:[self.orderTypeDic objectForKey:orderstateString]];
            [self.companyName setText:ordername];
            
            NSArray *orderProductArr = [dataDic objectForKey:@"orderProduct"];
            if ([ordertype integerValue] == 10) {
                orderProductArr = [dataDic objectForKey:@"goodsInfo"];
                addressString = [NSString stringWithFormat:@"%@ %@ \n %@",[dataDic objectForKey:@"consignee"],[dataDic objectForKey:@"phone"],[dataDic objectForKey:@"address"]];
            }
            
            if ([orderProductArr isKindOfClass:[NSArray class]]) {
                self.productArray = [orderProductArr mutableCopy];
                self.onetabbottomheight.constant = self.productArray.count * 44 + 20;
                [self.orderTable reloadData];
                
                
                CGFloat countAddFla = 0.00;
                for (NSDictionary *pricItem in orderProductArr) {
                    CGFloat itPric = [[pricItem objectForKey:@"quantity"] floatValue];
                    CGFloat itCount = [[pricItem objectForKey:@"unitprice"] floatValue];
                    countAddFla =  countAddFla + itPric * itCount;
                }
                NSString *counStringAdd = [NSString stringWithFormat:@"实付 ¥%.2f",countAddFla];
                [self.counPricLabel setHidden:NO];
                [self.counPricLabel setText:counStringAdd];
            }
            
            self.detalArray = [[NSMutableArray alloc]initWithObjects:orderdenumber,addtime,pplayType,addressString, nil];
            [self.detalsTable reloadData];
            
        }else{
            [SVProgressHUD showErrorWithStatus:msg];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}
- (NSString *)getyYearsTimeFormat:(NSString *)dateString{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateString integerValue]];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *timeStr = [df stringFromDate: date];
    //    NSString *timeStr = [df stringFromDate:date];
    return timeStr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.orderTable) {
        return self.productArray.count;
    }else{
       return self.detalArray.count;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.detalsTable) {
        if (indexPath.row == 3) {
            return 88;
        }else{
            return 44;
        }
    }else{
        return 44;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.orderTable) {
        static NSString *Identitfer  = @"OtherCell";
        OrderProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identitfer];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderProductTableViewCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *itemdic = [self.productArray objectAtIndex:indexPath.row];
        [cell.itemTitle setText:[itemdic objectForKey:@"productname"]];
        [cell.itemValue setText:[NSString stringWithFormat:@"x%@ ¥%@",[itemdic objectForKey:@"quantity"],[itemdic objectForKey:@"unitprice"]]];
        return cell;
    }else{
        static NSString *Identitfer  = @"Cell";
        RentalDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identitfer];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"RentalDetailsTableViewCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.itemLabel setText:[self.tableTitleArray objectAtIndex:indexPath.row]];
        [cell.valueLabel setText:[self.detalArray objectAtIndex:indexPath.row]];
        return cell;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)contentEvent:(UIButton *)sender {
    
}

- (IBAction)aginEvent:(UIButton *)sender {
    
}

- (NSMutableArray *)productArray{
    if (!_productArray) {
        _productArray = [[NSMutableArray alloc]init];
    }
    return _productArray;
}

- (NSMutableArray *)detalArray{
    if (!_detalArray) {
        _detalArray = [[NSMutableArray alloc]init];
    }
    return _detalArray;
}

@end
